local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),"HomeButton_"..script.Name);

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

    self.player=self.parent
	self.DataStore=self.parent:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
    self.ConList={}
    self.isBattle=false

    if  self:isHadPet() then
        self:AddProcess(function()
            self:dtor()
        end)

    else

        self.BeamLineCtrl=self.parent:GetComponent(ClientOHOH.load.Client_BeamLineCtrl())
        local con= self.parent.BattleSateEvent:Connect(function(isBattle)
            self.isBattle=isBattle
            if  not  self.isBattle then
                self:Update()
            end
        end)
    
        table.insert( self.ConList,con)
        local con2= self.parent.UserDataEvent:Connect(function(data)
           
            if data["base"] then
                self:Update()
            end
            if data["pets"] then
                if self:isHadPet() then
                    self:CloseLine()
                end
            end
        end)
        table.insert(self.ConList,con2)
    end


end;



function M.isHadPet(self)

    local data=self.DataStore:GetData("pets")
    for k,v in pairs(data.had) do
        return true
    end

    for k,v in pairs(data.equip) do
        return true
    end

    return false
end

function M.ShowLine(self,part)

    self:AddProcess(function()
        while not self.player.character do
            self:Wait(1.5)
        end
        
        while not self.player.character.main_inst do
            self:Wait(1.5)
        end
        self.LineId="GUDIE"
        self.BeamLineCtrl:CreateLine(part,self.LineId)

    end)

end

function M.CloseLine(self)
    if self.LineId then
        self.BeamLineCtrl:DestoryLine(self.LineId)
    end
    self:dtor()
end

local minEggPart=ClientOHOH:GetPath_Loop("Guide.MinEggPart",workspace).Value
local minEggPrice=ClientOHOH:GetPath_Loop("Guide.MinEggPrice",workspace).Value

function M.GetMinEggData(self)
    return {
        tagPart= minEggPart,
        Price=minEggPrice
    }
end

function M.Update(self,level,rebirth)

    if self.LineId then
        return 
    end
    if self.isBattle then
        return 
    end

    if self.BeamLineCtrl:isHadLine("GUDIE") then
        return 
    end

    local minEggData=self:GetMinEggData()
    
    if  self.DataStore:GetData("base").wins<minEggData.Price then
        return
    end

    self:ShowLine(minEggData.tagPart)

end

function M.dtor(self)
     for k,v in pairs(self.ConList) do
        v:Disconnect()
     end
     self.ConList={}
	M.super.dtor(self);
end;

return M